package org.springside.core.dao;

import java.io.Serializable;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Criterion;
import org.springframework.dao.DataAccessException;
import org.springside.core.utils.GenericsUtils;

/**
 * springside自带数据库处理类
 * @author user
 *
 * @param <T>
 */
@SuppressWarnings("unchecked")
public class HibernateEntityDao<T> extends HibernateGenericDao
  implements EntityDao<T>
{
protected Class<T> entityClass = GenericsUtils.getSuperClassGenricType(getClass());

  protected Class<T> getEntityClass()
  {
    return this.entityClass;
  }

  public T get(Serializable paramSerializable)
  {
    return get(getEntityClass(), paramSerializable);
  }

  public List<T> getAll()
  {
    return getAll(getEntityClass());
  }

  public List<T> getAll(String paramString, boolean paramBoolean)
  {
    return getAll(getEntityClass(), paramString, paramBoolean);
  }

  public void removeById(Serializable paramSerializable)
  {
    removeById(getEntityClass(), paramSerializable);
  }

  public Criteria createCriteria(Criterion[] paramArrayOfCriterion)
  {
    return createCriteria( paramArrayOfCriterion);
  }

  public Criteria createCriteria(String paramString, boolean paramBoolean, Criterion[] paramArrayOfCriterion)
  {
    return createCriteria( paramString, paramBoolean, paramArrayOfCriterion);
  }

  public List<T> findBy(String paramString, Object paramObject)
  {
    return findBy(getEntityClass(), paramString, paramObject);
  }

  public List<T> findBy(String paramString1, Object paramObject, String paramString2, boolean paramBoolean)
  {
    return findBy(getEntityClass(), paramString1, paramObject, paramString2, paramBoolean);
  }

  public T findUniqueBy(String paramString, Object paramObject)
  {
    return findUniqueBy(getEntityClass(), paramString, paramObject);
  }

  public boolean isUnique(Object paramObject, String paramString)
  {
    return isUnique(getEntityClass(), paramObject, paramString);
  }

  public void evit(Object paramObject)
  {
    getHibernateTemplate().evict(paramObject);
  }

  public void saveOrUpdate(Object paramObject)
  {
    getHibernateTemplate().merge(paramObject);
  }

  public void update(Object paramObject)
  {
    getHibernateTemplate().update(paramObject);
  }

  public Serializable saveAndgetPk(Object paramObject)
  {
    try
    {
      Serializable localSerializable = getHibernateTemplate().save(paramObject);
      return localSerializable;
    }
    catch (DataAccessException localDataAccessException)
    {
      localDataAccessException.printStackTrace();
    }
    return Integer.valueOf(-1);
  }
}